(function(){
    var t;
    var amount;
    if (!checkConnect()) return;
    async function init(){
        var rtn = await loadManbers();
        if (rtn){
            var my = getMy();
            if (!my || !my.isReal){
                alert("被批转成为正式会员后才可以购买");
                return;
            }
            setLoading(1);
            var dai = await getDAIReserve();
            var matic = await getMATICReserve();
            var fee = await getConst("memberFeeRMB");
            var myTransactions = await getMyTransactions();
            setLoading(0);
            dai = parseFloat(format(dai));
            matic = parseFloat(format(matic));
            setNodeText("fee",fee);
            if(myTransactions.length>0){
                //如果有缴费记录
                var list = getNode("feeHistory");
                var line = list.children[0];
                clearAll(list);
                for(var i=0;i<myTransactions.length;i++){
                    var newLine = line.cloneNode(true);
                    newLine.children[0].children[0].innerText="时间："+timeFormat(myTransactions[i].time)+
                        " 支付金额：￥"+ myTransactions[i].amount.toNumber()+"元";
                    list.appendChild(newLine);
                }
            }
            //点击支付后的业务逻辑
            getNode("pay").onclick=async function(){
                if (!checkConnect()) return;
                amount = getNode("amount").value;
                if (amount<1 || amount>5){
                    alert("最多可以购买5组");
                    return;
                }
                if (dai<amount*2){
                    alert("交易所DAI的余额不足");
                    return;
                }
                if (matic<amount*0.3){
                    alert("交易所MATIC的余额不足");
                    return;
                }
    
                //因为微信支付需要验证域名，而我们的域名注册在国外没有ICP，因此只能用极速汉语现有的域名来实现支付
                t = time();
                if(confirm("确认支付"+amount*fee*1.02+"元吗，购买"+amount*2+"个DAI和"+amount*0.3+"个MATIC吗？")){
                    amount *= fee;
                    var url = "https://api.smwho.cn/";
                    //使用了原有程序的微信支付部分，可以修改为使用callAPI来调用，但工作量比较大。
                    var http = new XMLHttpRequest();
                    http.open("GET",url+"hytgdaopay.php?address="+publicParam.address+"&amount="+amount+"&time="+t);
                    http.onreadystatechange = function(){
                        if (http.status<400 && http.readyState==4){
                            try{
                                var data = JSON.parse(http.responseText);
                                if(navigator.userAgent.indexOf("MicroMessenger")>-1)
                                    setNodeText("msg","暂不支持在微信中的直接支付，必须在电脑或用另一部手机扫码才能进行支付。");
                                else
                                    setNodeText("msg","请使用微信扫码支付。");
                                display("pay",false);
                                display("after",true);
                                getNode("qrcode").src = "https://smwho.cn/"+data.url;
                                //等待支付结束
                                checkPayment();
                            }catch(e){
                            }
                        }
                    };
                    http.send();
                }
            };
        }
    }
    /**
    * 确认支付是否成功
    */
    function checkPayment(){
		var http = new XMLHttpRequest();
		http.open("GET","https://api.smwho.cn/hytgdaoPayment.php?address="+publicParam.address+"&time="+t);
		http.onreadystatechange = async function(){
			if (http.status<400 && http.readyState==4){
                if(http.responseText){
                    display("after",false);
                    setLoading(1);
                    const rtn = await newTransaction("购买代币，注意这里必须同意，否则无法完成转账！",http.responseText,amount);
                    if (rtn){
                        goto("wallet/balance");
                    }
                    setLoading(0);
                }else {
                    //每隔2秒检查一次是否支付成功
                    publicParam.checkPaymentTimer = setTimeout(checkPayment,2000);
                }
            }
		};
		http.send();
    }
    init();
})();